package com.syni.mdd.yhd.common.all.listen.business;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsUserOperationLog;
import com.syni.mdd.yhd.common.all.entity.groupChat.BmsUserChat;
import com.syni.mdd.yhd.common.all.service.BmsDxOrderService;
import com.syni.mdd.yhd.common.all.service.BmsUserOperationLogService;
import com.syni.mdd.yhd.common.all.service.groupChat.BmsUserChatService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import com.syni.mdd.yhd.common.all.utils.aurora.AuroraMessageUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
@Log4j2
public class BusinessUpdateDataListener implements ApplicationListener<BusinessUpdateDataEvent> {

    @Autowired
    BmsUserChatService bmsUserChatService;

    @Autowired
    BmsDxOrderService bmsDxOrderService;

    @Override
    public void onApplicationEvent(BusinessUpdateDataEvent businessUpdateDataEvent) {
        BmsBusiness bmsBusiness = businessUpdateDataEvent.getBmsBusiness();
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("roleId",Constants.BUSINESS_ROLE_ID);
        conditionMap.put("businessId",bmsBusiness.getId());
        List<BmsUserChat> bmsUserChats = bmsUserChatService.getBaseDao().findAll(new SpecificationUtils(conditionMap));
        if(bmsUserChats.size() > 0){
            AuroraMessageUtils.updateBusinessUsers(bmsBusiness,bmsUserChats.get(0).getUserId(),Constants.BUSINESS_ROLE_ID);
        }
        //更新订单信息
        bmsDxOrderService.updateOrderBusinessName(bmsBusiness);
    }
}
